﻿package com.tomvian.maths.areas
{
	import com.tomvian.maths.Vector2D;
	
	/**
	 * TorusArea
	 * @author Thomas Vian
	 */
	public class TorusArea extends Area
	{
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		private var _center:Vector2D;
		private var _innerRadius:Number;
		private var _outerRadius:Number;
		private var _innerRadiusSqr:Number;
		private var _outerRadiusSqr:Number;
		
		private var _radiusDiff:Number;
		
		private var _ratio:Number;
		private var _ratioGap:Number;
		
		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------
		
		public function TorusArea(center:Vector2D, innerRadius:Number, outerRadius:Number)
		{
			_center = center;
			
			_innerRadius = innerRadius;
			_outerRadius = outerRadius;
			
			_innerRadiusSqr = _innerRadius * _innerRadius;
			_outerRadiusSqr = _outerRadius * _outerRadius;
			
			area = Math.PI * (_outerRadiusSqr - _innerRadiusSqr);
			
			_radiusDiff = _outerRadius - _innerRadius;
			_ratio = _innerRadius / _outerRadius;
			_ratioGap = 1 - _ratio;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		override public function contains(vector:Vector2D):Boolean 
		{ 
			var lengthSqr:Number = vector.subtract(_center).lengthSqr;
			
			return (lengthSqr <= _outerRadius) && (lengthSqr >= _innerRadius);
		}
		
		override public function random():Vector2D 
		{
			var rand:Number = Math.random() * _ratioGap + _ratio;
			return Vector2D.newFromAngle(Math.random() * Math.PI * 2, rand * rand * _radiusDiff + _innerRadius).addMod(_center);
		}
	}
}